<?php
namespace app\platform\middleware;
use app\common\logic\TokenLogic;
use app\common\model\account\AccountModel;

class AccountCheck{
    use \app\BaseTrait;
    /**
     * 处理请求
     *  验证是否登录是否正确的中间件
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $auth_token = $request->param('auth_token');
        if(empty($auth_token)){
            return $this->error('未登录',100);
        }
        $res = TokenLogic::getAccountData($auth_token);
        if(false == $res){
            return $this->error('未登录',100);
        }
        if(empty($res['accountId'])){
            return $this->error('未登录',100);
        }
        if(empty($res['code'])){
            return $this->error('未登录',100);
        }
        if(null === $account= AccountModel::find($res['accountId'])){
            return $this->error('账户不存在',100);
        }
        if($account->is_delete != 0){
            return $this->error('账户不存在',100);
        }
        if($account->status != 1){
            return $this->error('账户异常',100);
        }
        if($account->code != $res['code']){
            return $this->error('账户不存在',100);
        }
        $request->accountId=$account->account_id;
        $request->account=$account;

        return $next($request);
    }
}